// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HelperTest.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Tests
{
    using System;
    using System.Linq;
    using System.Net.NetworkInformation;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    using ExecutionTool.Common;

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Win32;

    [TestClass]
    public class HelperTest
    {
        #region Public Methods and Operators

        [TestMethod]
        public void CanGetNetworkInterface()
        {
            var nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var nic in nics.Where(n => n.OperationalStatus == OperationalStatus.Up))
            {
                var mac = nic.GetPhysicalAddress();
                Console.WriteLine(nic.Id);
                Console.WriteLine(nic.Name);
                Console.WriteLine(nic.NetworkInterfaceType);
                Console.WriteLine(mac.ToString());
                Console.WriteLine("----");
            }

            Console.WriteLine("IntPtr.Size: {0}", IntPtr.Size);
            var on64 = IntPtr.Size == 8;

            RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, on64 ? RegistryView.Registry64 : RegistryView.Registry32);

            string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
            var rpk = key.OpenSubKey(keyPath).GetValue("ProductId").ToString();

            Console.WriteLine(rpk);
            Console.WriteLine(rpk.Length);
            var pidBytes = Encoding.ASCII.GetBytes(rpk);
            Console.WriteLine(pidBytes.Length);

            var tripleDesCryptoServiceProvider = new TripleDESCryptoServiceProvider();
            Console.WriteLine(tripleDesCryptoServiceProvider.KeySize);
            Console.WriteLine(tripleDesCryptoServiceProvider.Key.Length);
            Console.WriteLine(tripleDesCryptoServiceProvider.BlockSize);
            Console.WriteLine(tripleDesCryptoServiceProvider.IV.Length);
            tripleDesCryptoServiceProvider.GenerateKey();
            tripleDesCryptoServiceProvider.GenerateIV();
            Console.WriteLine(string.Join(",", tripleDesCryptoServiceProvider.Key));
            Console.WriteLine(string.Join(",", tripleDesCryptoServiceProvider.IV));

            var clear = "I'm Jack Ma";
            var ciphered = Helper.Encrypt(clear);
            Console.WriteLine(ciphered);
            var clearAgain = Helper.Decrypt(ciphered);

            Assert.AreEqual(clear, clearAgain);
        }

        [TestMethod]
        public void TestLogWriterReferenceThroughUnityContainer()
        {
            IUnityContainer container = new UnityContainer();
            var locator = new UnityServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => locator);
            container.AddNewExtension<EnterpriseLibraryCoreExtension>();

            var logger = ServiceLocator.Current.GetInstance<LogWriter>();
            var logger2 = ServiceLocator.Current.GetInstance<LogWriter>();

            Assert.AreSame(logger, logger2, "The same reference on the same thread. " + Thread.CurrentThread.ManagedThreadId);

            LogWriter logger3 = null;
            var t = Task.Factory.StartNew(() => { logger3 = ServiceLocator.Current.GetInstance<LogWriter>(); });
            t.Wait();

            Assert.AreSame(logger, logger3, "The same reference on different thread. " + Thread.CurrentThread.ManagedThreadId);
        }

        #endregion
    }
}
